#include"../include/workque.h"
#include "../include/thread_Pool.h"
#include "../include/head.h"
void cleanFunc(void *p)
{
    pQue_t pQue = (pQue_t)p;
    
    //解锁
    pthread_mutex_unlock(&pQue->mutex);
}
void *threadFunc(void *p)
{
   
    pQue_t pQue = (pQue_t)p;
    pNode_t pCur;
    int getsuccess = 1;
    while(1)
    {
        //pthread_mutex_lock(&pQue->mutex);
        //使用线程终止清理函数清理锁资源
       
        if(0 == pQue->size)
        {
            //等待队列里面有任务
            
            pthread_cond_wait(&pQue->cond, &pQue->mutex);
        }
        getsuccess = queGet(pQue, &pCur);
        pthread_mutex_unlock(&pQue->mutex);
        if(0 == getsuccess)
        {
            if(!(strcmp(pCur->order,"gets")))
            {
               
                transFile(pCur->clientFd,pCur->file);
            }
            else
            {
                serverGets(pCur->clientFd,pCur->parent_id,pCur->userid);
            }
            free(pCur);
            pCur = NULL;
        }
       
    }
    pthread_exit(NULL);
    
   
}

int threadPoolInit(pThreadPool_t pPool, int threadNum)
{
    pPool->threadNum = threadNum;
    pPool->pthid = (pthread_t*)calloc(threadNum, sizeof(pthread_t));
    queInit(&pPool->Que);
    return 0;
}

int threadPoolStart(pThreadPool_t pPool)
{
    
    for(int i = 0; i < pPool->threadNum; ++i)
    {
        pthread_create(pPool->pthid+i, NULL, threadFunc, &pPool->Que);
    }
    return 0;
}